package com.software.frontend.mine.adapters;

import static ly.img.android.pesdk.backend.decoder.ImageSource.getResources;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.constraintlayout.widget.ConstraintLayout;

import com.software.frontend.R;
import com.software.frontend.mine.entity.Comment;
import com.software.frontend.mine.entity.Like;
import com.software.frontend.mine.entity.SystemNotice;
import com.software.frontend.mine.utils.PictureProcessUtils;

import java.util.List;

public class SystemNoticeAdapter extends BaseAdapter {

    LayoutInflater inflater;
    List<SystemNotice> data;

    public SystemNoticeAdapter(Context context, List data) {
        inflater=(LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        this.data = data;
    }

    @Override
    public int getCount() {
        return data.size();
    }

    @Override
    public Object getItem(int i) {
        return data.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @SuppressLint("SetTextI18n")
    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        SystemNotice notice = data.get(i);
        if (view == null) {
            view = (ConstraintLayout)inflater.inflate(R.layout.item_notices_system, viewGroup,false);
        }
        //需要判断传入的Comments是否合法
        if (isNoticeLegal(notice)) {
            TextView timeLate = (TextView) view.findViewById(R.id.textView8);
            if (notice.getTimeLate() == 0){
                timeLate.setText("刚刚");
            }else {
                timeLate.setText(notice.getTimeLate() + "分钟前");
            }
            ImageView HandlerHead = (ImageView)view.findViewById(R.id.imageView);
            HandlerHead.setImageBitmap(((BitmapDrawable)(getResources().getDrawable(R.drawable.logo))).getBitmap());
            TextView textViewComment = (TextView)view.findViewById(R.id.textView9);
            textViewComment.setText(notice.getNotice());
        }else {
            errorHandle();
        }

        return view;
    }

    private boolean isNoticeLegal(SystemNotice notice) {
        if (notice.getNotice() != null && !notice.getNotice().equals("")
                && notice.getTimeLate() >= 0){
            return true;
        }else {
            return false;
        }

    }

    private void errorHandle() {
        //错误处理
    }
}
